// pages/search/search.js
Page({
  data: {
    searchValue: '',
    historySearchList: []
  },
  onLoad() {
    const historySearchList = wx.getStorageSync('historySearchList')
    this.setData({historySearchList})
  },
  inputChange(ev) {
    this.setData({
      searchValue: ev.detail.value
    })
  },
  search() {
    const historySearch = this.data.searchValue
    if (historySearch !== '') {
      const historySearchList = [...this.data.historySearchList, historySearch]
      wx.setStorageSync('historySearchList', historySearchList)
      this.setData({historySearchList})
    }
    wx.redirectTo({
      url: `../goodslist/goodslist?goodsName=${this.data.searchValue}`,
    })
  },
  deleteSearch() {
    this.setData(
      {historySearchList: []}
    )
    wx.setStorageSync('historySearchList', [])
  },
  changeSearch(item) {
    const searchValue = item.target.dataset.value
    this.setData({searchValue})
    wx.navigateTo({
      url: `../goodslist/goodslist?goodsName=${this.data.searchValue}`,
    })
  }
})